package com.music.controller;

import com.music.core.RedisCache;
import com.music.myenum.SmsType;
import com.music.service.SmsService;
import com.music.util.JsonResultUtil;
import com.music.util.PhoneFormatCheckUtils;
import com.music.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.net.InetAddress;
import java.net.UnknownHostException;

// 发送短信中心
@RestController
@RequestMapping("/sms")
@CrossOrigin
public class SmsController {

	@Value("${server.port}")
	private int serverPort;

	@Autowired
	private RedisCache redisCache;

	@Autowired
	private SmsService smsService;

	/**
	 * 发送手机短信
	 * @param phone
	 * @return
	 */
	@RequestMapping("/send")
	public JsonResultUtil send(@RequestBody String phone) {
		String code = redisCache.getCacheObject(phone);
		if(StringUtils.hasLength(code)) {

			JsonResultUtil jsonResultUtil = new JsonResultUtil();
			jsonResultUtil.setCode(200);
			return jsonResultUtil;
		}

		// 验证手机号
		boolean bool = PhoneFormatCheckUtils.isChinaPhoneLegal(phone);

		if(!bool) {
			JsonResultUtil jsonResultUtil = new JsonResultUtil();
			jsonResultUtil.setCode(200);
			jsonResultUtil.setMsg("手机号非法");
			return jsonResultUtil;
		}

		// 生成验证码给第三方进行发送
		code = RandomUtil.getSixRandom();

		//调用service中发送短信的方法
		boolean isSend = smsService.sendSMS(SmsType.PHONE,phone, code);
		if (isSend){
			//如果发送成功，把发送成功的code验证码保存到redis中，并设置有效时间，设置5分钟过期
			redisCache.setCacheObject(phone, code);
//			redisCache.setCacheObject(phone, code, 5, TimeUnit.MINUTES);
			JsonResultUtil jsonResultUtil = new JsonResultUtil();
			jsonResultUtil.setCode(200);
			return jsonResultUtil;
		}

		JsonResultUtil jsonResultUtil = new JsonResultUtil();
		jsonResultUtil.setCode(201);
		jsonResultUtil.setMsg("短信发送失败");
		return jsonResultUtil;
	}


	/**
	 * 发送QQ验证码
	 * @param qqNumber
	 * @return
	 */
	@RequestMapping("/sendEmail")
	public JsonResultUtil sendEmail(@RequestBody String qqNumber) {
		String code = redisCache.getCacheObject(qqNumber);
		if(StringUtils.hasLength(code)) {

			JsonResultUtil jsonResultUtil = new JsonResultUtil();
			jsonResultUtil.setCode(200);
			return jsonResultUtil;
		}

		// 生成验证码给第三方进行发送
		code = RandomUtil.getSixRandom();

		try {
			boolean flag = smsService.sendSMS(SmsType.QQEMAIL,qqNumber, code);

//			mailSender.send(message); // 不抛出异常就是成功的
			redisCache.setCacheObject(qqNumber, code);
//			redisCache.setCacheObject(qqNumber, code, 10, TimeUnit.MINUTES);
			System.out.println("发送成功" + qqNumber);

			JsonResultUtil jsonResultUtil = new JsonResultUtil();
			jsonResultUtil.setCode(200);
			return jsonResultUtil;
		} catch (MailException e) {
			e.printStackTrace();

			JsonResultUtil jsonResultUtil = new JsonResultUtil();
			jsonResultUtil.setCode(201);;
			return jsonResultUtil;
		}
	}

	/**
	 * 测试控制层
	 * @param phone
	 * @return
	 */
	@RequestMapping("/test/send")
	public JsonResultUtil sendTest(@RequestBody String phone) {
		JsonResultUtil jsonResultUtil = new JsonResultUtil();
		jsonResultUtil.setCode(200);
		try {
			InetAddress localHost = InetAddress.getLocalHost();
			jsonResultUtil.setMsg("请求地址:" + localHost.getHostAddress() + " 请求端口:" + this.serverPort + " 手机号：" + phone);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return jsonResultUtil;
	}


}
